//
//  NowDate.m
//  WinnerLife
//
//  Created by 李小龙 on 14-12-22.
//  Copyright (c) 2014年 李小龙. All rights reserved.
//

#import "DateSomeThing.h"

@implementation DateSomeThing
+(NSString *)getNowDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy年MM月dd日";
    NSString *date = [formatter stringFromDate:[NSDate date]];
    return date;
}

+(NSString *)getNowDateAll
{
    NSDateFormatter *forMatter = [[NSDateFormatter alloc]init];
    forMatter.dateFormat = @"yyyy年MM月dd日 HH.mm.ss";//HH:mm:ss作为txt的名字 符号不行 自动变成"/" 用"/"存储时会把"/"当成文件夹路径 存不上
    NSString *date = [forMatter stringFromDate:[NSDate date]];
    return date;
}
+(NSString *)GetDay:(NSDate *)aDate gap:(NSInteger)gap returnDateFormat:(NSString *)dateFormatStr{
    if (gap == 0) {
        return @"今天";
    }
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:aDate];
    [components setDay:([components day]-gap)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    return [DateSomeThing getDateStr:beginningOfWeek dateFormat:dateFormatStr];
}
+(NSString *)getDateStr:(NSDate *)date dateFormat:(NSString *)dateFormat
{
    NSDateFormatter *forMatter = [[NSDateFormatter alloc]init];
    forMatter.dateFormat = dateFormat;
    NSString *dateStr = [forMatter stringFromDate:date];
    return dateStr;
}
+(NSDate *)getDateWithString:(NSString *)dateStr dateFormat:(NSString *)dateFormat
{
    NSDateFormatter *forMatter = [[NSDateFormatter alloc]init];
    forMatter.dateFormat = dateFormat;
    NSDate *date = [forMatter dateFromString:dateStr];
    return date;
}
+(NSDate *)getDateWithTimestampstr:(NSString*)timestampstr {
    NSTimeInterval a = [timestampstr doubleValue];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:a];
    return confromTimesp;
}
+(NSString *)getDateStrWithTimestampstr:(NSString*)timestampstr formatStr:(NSString *)formatStr{
    NSTimeInterval a = [timestampstr doubleValue];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:a];
    return [DateSomeThing getDateStr:confromTimesp dateFormat:formatStr];
}
+(DateSomeThing *)createWithString:(NSString *)dateStr dateFormat:(NSString *)dateFormat
{
    DateSomeThing *dateSome = [[DateSomeThing alloc]init];
    NSDate *date = [DateSomeThing getDateWithString:dateStr dateFormat:dateFormat];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:date];
    
    /**
     *  iOS8之前的
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];//NSCalendarIdentifierGregorian
    NSDateComponents *comps = [[NSDateComponents alloc]init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:date];
    */
    
    dateSome.year = [comps year];
    dateSome.month = [comps month];
    dateSome.day = [comps day];
    NSInteger aWeek = [comps weekday];
    NSString *weekStr;
    switch (aWeek) {
        case 1:
            weekStr = @"星期日";
            break;
        case 2:
            weekStr = @"星期一";
            break;
        case 3:
            weekStr = @"星期二";
            break;
        case 4:
            weekStr = @"星期三";
            break;
        case 5:
            weekStr = @"星期四";
            break;
        case 6:
            weekStr = @"星期五";
            break;
        case 7:
            weekStr = @"星期六";
            break;
        default:
            break;
    }
    dateSome.week = weekStr;
    dateSome.hour = [comps hour];
    dateSome.min = [comps minute];
    dateSome.sec = [comps second];
    
    return dateSome;
}
+(DateSomeThing *)getWithStartDateStr:(NSString *)startStr startDateFormat:(NSString *)startFormatStr endDateStr:(NSString *)endStr endDateFormat:(NSString *)endFormatStr {
    DateSomeThing *dateSome = [[DateSomeThing alloc] init];
    NSDate *startDate = [DateSomeThing getDateWithString:startStr dateFormat:startFormatStr];
    NSDate *endDate   = [DateSomeThing getDateWithString:endStr dateFormat:endFormatStr];
    
    // 日历对象 （方便比较两个日期之间的差距）
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // NSCalendarUnit 枚举代表想获得哪些差值
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitWeekOfMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 计算两个日期之间的差值
    NSDateComponents *cmps = [calendar components:unit fromDate:startDate toDate:endDate options:0];
//    // 获得某个时间的年月日时分秒
//    NSDateComponents *createDateCmps = [calendar components:unit fromDate:startDate];
//    NSDateComponents *endCmps = [calendar components:unit fromDate:endDate];
    
    dateSome.year  = [cmps year];
    dateSome.month = [cmps month];
    dateSome.day   = [cmps day];
    dateSome.hour  = [cmps hour];
    dateSome.min   = [cmps minute];
    dateSome.sec   = [cmps second];
    return dateSome;
}

+(NSString *)getStringWithStartDateStr:(NSString *)startStr startDateFormat:(NSString *)startFormatStr endDateStr:(NSString *)endStr endDateFormat:(NSString *)endFormatStr {
    
    DateSomeThing *dateSome = [DateSomeThing getWithStartDateStr:startStr startDateFormat:startFormatStr endDateStr:endStr endDateFormat:endFormatStr];
    
    NSString *differStr = [NSString stringWithFormat:@"剩余：%ld天%ld小时%ld分%ld秒",dateSome.day,dateSome.hour,dateSome.min,(long)dateSome.sec];
    return differStr;
}
+(NSString *)getStringWithCurrentDateStr:(NSString *)currentStr currentDateFormat:(NSString *)currentFormatStr {
    NSString *nowStr = [DateSomeThing getNowDateAll];
    
    DateSomeThing *dateSome = [DateSomeThing getWithStartDateStr:nowStr startDateFormat:@"yyyy年MM月dd日 HH.mm.ss" endDateStr:currentStr endDateFormat:currentFormatStr];
    
    NSString *differStr = [NSString stringWithFormat:@"剩余：%ld天%ld小时%ld分%ld秒",dateSome.day,dateSome.hour,dateSome.min,(long)dateSome.sec];
    return differStr;
}

+(double)getStringWithCurrentTimestamp:(NSString *)timestamp {
    NSString *nowStr = [DateSomeThing getNowDateAll];
    
    DateSomeThing *dateSome = [DateSomeThing getWithStartDateStr:nowStr startDateFormat:@"yyyy年MM月dd日 HH.mm.ss" endDateStr:[DateSomeThing getDateStrWithTimestampstr:timestamp formatStr:@"yyyy-MM-dd HH:mm:ss"] endDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return (dateSome.hour*60*60+dateSome.min*60+dateSome.sec);
}

+(double)getStringWithCurrentTimeStr:(NSString *)timeStr currentDateFormat:(NSString *)currentFormatStr {
    NSString *nowStr = [DateSomeThing getDateStr:[NSDate date] dateFormat:currentFormatStr];
    NSString * _time = timeStr;
    if (_time.length > 2) {
        if ([[_time substringToIndex:2] isEqualToString:@"24"]) {
            _time = @"23:59:59";
        }
    }
    DateSomeThing *dateSome = [DateSomeThing getWithStartDateStr:nowStr startDateFormat:currentFormatStr endDateStr:_time endDateFormat:currentFormatStr];
    return (dateSome.hour*60*60+dateSome.min*60+dateSome.sec);
}

+(NSString *)getDateStr:(NSString *)inStr inDateFormat:(NSString *)inFormatStr outDateFormat:(NSString *)outFormatStr
{
    NSDate *inDate = [DateSomeThing getDateWithString:inStr dateFormat:inFormatStr];
    NSString *outDateStr = [DateSomeThing getDateStr:inDate dateFormat:outFormatStr];
    
    return outDateStr;
}

+(NSString*)timestampToTimeStringWithFormat:(NSString*)str timestampstr:(NSString*)timestampstr
{
    //还原时间
    NSTimeInterval a = [timestampstr doubleValue];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:a];
    //    NSLog(@"时间= %@",confromTimesp); //之后就可以对NSDate进行格式或处理
    
//    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
//    //    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
//    [inputFormatter setDateFormat:str];
//    inputFormatter.timeZone = [NSTimeZone systemTimeZone];
//    NSString* nowtime = [inputFormatter stringFromDate:confromTimesp];
    return [DateSomeThing getDateStr:confromTimesp dateFormat:str];
    
}

+(NSString *)getTimestampDateStr:(NSString *)dateStr format:(NSString *)format {
    NSDate *date = [DateSomeThing getDateWithString:dateStr dateFormat:format];
    return [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
}

@end
